package com.github.rpc4j.producer.service;

import net.sf.cglib.reflect.FastMethod;

import com.github.rpc4j.common.ServiceRequest;
import com.github.rpc4j.producer.exception.Rpc4jServiceException;
import com.github.rpc4j.producer.util.Utils;


public class Rpc4jService {
	
	private Service service;
	
	public Rpc4jService(Service service){
		this.service = service;
	}

	public Service getService() {
		return service;
	}
	
	public Object invoke(ServiceRequest request) throws Throwable{
		Object object = service.getTarget();
		FastMethod fmethod = null;
		try{
			String requestMethodKey = Utils.getServiceRequestMethodKey(request); 
			fmethod = service.getMethodMap().get(requestMethodKey);
			if(fmethod==null)
				throw new Rpc4jServiceException("server not method"+request.getMethod());
			return fmethod.invoke(object, request.getServiceArgs());
		}catch(Throwable e){
			throw e;
		}
	}
}
